/**
 * 游戏主场景
 *
 */
class GmaeMian extends eui.Component {
    public kano: Kano;
    public bg: eui.Image
    public computerLogic: ComputerLogic;
    public input: eui.TextInput;
    public label: eui.Label;

    private timer: number;
    private _instructArr = [2,3,4,5,6,7,8,9,10,20,21,22,23,24,25,26];
    private _tonArr: any[] = [];
    public constructor() {
    	   super();
    	   this.skinName = "GameSceneSkin"
        this.start();
    }
    public start() {
        this.bg.touchEnabled = true;
        this.bg.addEventListener(egret.TouchEvent.TOUCH_TAP,this.onTouch,this);
        this.computerLogic = new ComputerLogic();
        this.onComputer();
        var myself = this;
        document.addEventListener("keydown",function(event: KeyboardEvent) {
            var bool = true;
            for(var i = 0;i < myself._tonArr.length;i++) {
                if(myself._tonArr[i] == event.keyCode) {
                    bool = false;
                }
            }
            if(bool) {//去除重复按键
                myself._tonArr.push(event.keyCode);
            }
            for(var i = 0;i < myself._instructArr.length;i++) {
                var arr: string[] = (DataConfig.kano[myself._instructArr[i]].control).split("|");
                var key: string = myself._tonArr.toString();
                for(var n = 0;n < arr.length;n++) {
                    if(key == arr[n]) {
                        myself.kano.instruct = myself._instructArr[i];
                    }
                }
            }
            console.log("键盘按钮" + myself._tonArr);
        });
        document.addEventListener("keyup",function(event: KeyboardEvent) {
            var bool = true;
            for(var i = 0;i < myself._tonArr.length;i++) {
                if(myself._tonArr[i] == event.keyCode) {
                    myself._tonArr[i] = null;
                    tool.SetDataTool.removeElement(i,myself._tonArr);
                }
            }
            console.log("释放键盘" + myself._tonArr);
            myself.kano.exeDefaultAction();
        });
        this.kano = new Kano();
        this.addChild(this.kano);
        
        //---------------------------------------------
        
    }
    private onComputer() {
        this.computerLogic.addCom();
        var funcString: string = this.computerLogic.multiplier.toString() + "+" + this.computerLogic.multiplicand.toString();
        this.label.text = funcString;
        this.bg.touchEnabled = true;
    }
    private onTouch() {
        this.bg.touchEnabled = false;
        if(this.input.text != "" && this.input.text != null) {
            if(this.computerLogic.onBingo(parseInt(this.input.text))) {
                this.label.text = "Bingo";
                this.kano.instruct = 30;
            }
            else {
                this.label.text = "Wrong";
                this.kano.instruct = 31;
            }
        }
        else {
            this.bg.touchEnabled = true;
            return;
        }
        this.timer = egret.setTimeout(this.onComputer,this,1000);
    }
}
